import QtQuick
import QtQuick.Window

import QtQuick.Controls
import FlatUIControls 1.0

Window {
    id: main
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")



    Column {
        x:10
        spacing: 2

        SerialPortPanel {
            id: serialConsoleView
        }

        Item {
            width: 320
            height: 200
            ScrollView {
                anchors.fill: parent
                MultiLineInput {
                    id: msgDisplay
                }
            }
        }

        Row {
            spacing: 2
            SingleLineInput{
                id: msg_input
                width: 260
                height: 40
            }
            ImageButton {
                id: send_btn
                width: 58
                text: qsTr("发送")
                onClicked: {
                    sendMessage();
                }
            }
        }
    }


    function sendMessage() {
        var msg = msg_input.text;
        control.sendMessage(msg);
    }

    Connections {
        target: serialConsoleView
        function onSigRequestDeviceList() {
            control.updateDeviceList();
        }
        function onSigConnect(com) {
            control.connectDevice(com);
        }
        function onSigDisconnect() {
            control.disConnectDevice();
        }
    }

    Connections {
        target: control
        function onDeviceListChanged(devlist) {
            serialConsoleView.refreshDeviceList(devlist);
        }
        function onDeviceStatusChanged(isConnected) {
            serialConsoleView.refreshComState(isConnected);
        }
        function onDeviceRxIndication(message) {
            msgDisplay.append(message);
        }
    }

    Component.onCompleted: {
    }
}
